package  
{
	import net.flashpunk.Entity;
	import net.flashpunk.graphics.Image;
	import net.flashpunk.Graphic;
	import net.flashpunk.FP;
	import net.flashpunk.masks.Pixelmask;
	import net.flashpunk.utils.Input;
	
	/**
	 * © 2012 TheJavaArcade.com
	 */	
	public class Ball extends Entity{
		
		public var ball_sprite:Image = new Image(Assets.BALL_SPRITE);
		
		public var x_velocity:int;
		public var y_velocity:int;
		
		public var vX:Number;
		public var vY:Number;
		
		public function Ball(){
			graphic = ball_sprite;
			layer = 0;
			mask = new Pixelmask(Assets.BALL_SPRITE);
			x = 100; y = 100;
			x_velocity = 2;
			y_velocity = -2;
			type = "ball";
		}
		
		override public function update():void {
			//pre-launch ball
			if (!GameWorld.ballOut && !GameWorld.gameOver && !GameWorld.isBonus) {
				vX = Input.mouseX - 300;
				vY = Input.mouseY - 250;
				if (vX == 0 && vY == 0) vX = 1;
				var mV:Number = Math.sqrt(vX * vX + vY * vY);
				var ballX:Number = 300 + (vX / mV * 75);
				var ballY:Number = 250 + (vY / mV * 75);
				x = ballX-4;
				y = ballY-4;
			}
			//launch the ball
			if (Input.mousePressed) {
				if (!GameWorld.ballOut && !GameWorld.isBonus && !GameWorld.gameOver) {
					GameWorld.paddle.mX = Input.mouseX;
					GameWorld.paddle.mY = Input.mouseY;
					GameWorld.ballOut = true;
					GameWorld.clickText.visible = false;
					//set ball's initial velocity
					x_velocity = vX / 30;
					if (x_velocity == 0 && x < 290) x_velocity = -1;
					if (x_velocity == 0 && x > 310) x_velocity = 1;
					if (x_velocity > 8) x_velocity = 8;
					if (x_velocity < -8) x_velocity = -8;
					
					y_velocity = vY / 30;
					if (y_velocity == 0 && y < 240) y_velocity = -1;
					if (y_velocity == 0 && y > 260) y_velocity = 1;
					if (y_velocity > 8) y_velocity = 8;
					if (y_velocity < -8) y_velocity = -8;
				}
			}
			//ball action
			if (GameWorld.ballOut && !GameWorld.gameOver) {
				//wall collision	
					if (collide("wall", x + x_velocity, y)) {
						x_velocity = x_velocity * -1;
						if (x_velocity < -1) x_velocity = x_velocity +1;
						if (x_velocity > 1) x_velocity = x_velocity -1;
					}
					if (collide("wall", x, y + y_velocity)) {
						y_velocity = y_velocity * -1;
						if (y_velocity < -1) y_velocity = y_velocity +1;
						if (y_velocity > 1) y_velocity = y_velocity -1;
					}
				//paddle collision
					if (collide("paddle", x + x_velocity, y)) {
						x_velocity = x_velocity * -1;
						if (x_velocity < 0 && GameWorld.paddle.x_velocity < 0) x_velocity = x_velocity - 1;
						if (x_velocity > 0 && GameWorld.paddle.x_velocity > 0) x_velocity = x_velocity + 1;
					}
					if (collide("paddle", x, y + y_velocity)) {
						y_velocity = y_velocity * -1;
						if (y_velocity < 0 && GameWorld.paddle.y_velocity < 0) y_velocity = y_velocity - 1;
						if (y_velocity > 0 && GameWorld.paddle.y_velocity > 0) y_velocity = y_velocity + 1;
					}
					x = x + x_velocity;
					y = y + y_velocity;
				}
			}
		
	}

}